package com.kfc.crazy4week.api.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.bean.validators.plugins.schema.NotBlankAnnotationPlugin;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author weichenxu
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Value("${swagger.enable:false}")
    private String enable;

    @Bean
    public Docket commonApiDoc() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo("疯狂星期四项目"))
                .groupName("公共服务")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kfc.crazy4week"))
                .paths(PathSelectors.any())
                .build()
                .enable("true".equals(enable));
    }

    @Bean
    public NotBlankAnnotationPlugin notBlankAnnotationPlugin() {
        return new NotBlankAnnotationPlugin();
    }


    private ApiInfo getApiInfo(String title) {
        return new ApiInfoBuilder()
                .title(title)
                .description("后端API文档")
                .version("1.0")
                .contact(new Contact("wcx", null, null))
                .termsOfServiceUrl(BELIEF)
                .build();
    }

    /**
     * QAQ
     */
    private static final String BELIEF = "If there is a mistake, it must be the fault of the front-end developer. (•̀ᴗ•́)و ̑̑ ";
}
